#include <vector>
#include <iostream>
#include <string>
#include "Constants.h"
#include "TGraph.h"
#include <set>
#include <vector>
#include <unordered_map>
#include "RoadConSocial.h"
#include "TestGrady.h"
using namespace std;

int main() {
	
	// ----------------------------CTS--------------------------------- 
	float delta = 300;
	int topK = 20;
	RoadGraph* road_graph = new RoadGraph(delta);

	//cout << "road_graph->count_edge = " << road_graph->count_edge << endl;

	SocialGraph* social_graph = new SocialGraph();

	RoadConSocial* road_social = new RoadConSocial(road_graph, social_graph, delta , topK); 

	fstream fin(social_query);
	ofstream fout(gen_qRegion);
	ofstream fout1(countTopK_CTS);
	int query_num;
	fin >> query_num;
	int nodeId, start_time;
	float total_query = 0;
	float total_topk = 0;
	float time_bmax = 0;
	int count = 0;
	int q_sum = 0;
	while (fin >> nodeId >> start_time) {
		topK = 20;
		Rectangle* qRect = road_social->getQMBR(nodeId , start_time , delta, 900 , total_query, fout , time_bmax);
		q_sum += road_social->qRegion.size();
		unordered_set<int> inner_com = road_social->secureRangeQuery(qRect);
		
		clock_t tbegin = clock(), tend;
		list<pair<int, int>> topk_all = road_social->getTopKFromAll(inner_com, qRect, topK);
		list<pair<int, int>> topk_part = road_social->getTopKFromPart(inner_com, qRect, topK); 
		list<pair<int, int>> topk = road_social->combineAll_PartTopK(topk_all, topk_part, topk_part.size());
		tend = clock();

		for (int id = 0; id < social_graph->node_num; id++) {
			social_graph->nodes[id]->binf = 0;
			social_graph->nodes[id]->canInfulence.clear();
			social_graph->nodes[id]->beInfluence.clear();
		}

		total_topk += tend - tbegin;
		int sum = 0;
		for (auto node : topk) {
			sum += node.second;
		}
		fout1 << sum << endl;
		
	}
	cout << "bidRearch cost time = " << total_query / 1000.0 << " ms   " << "topk cost = " << total_topk / 1000.0 << " ms   total = " << (total_query + total_topk) / 1000.0 << "ms" << endl;
	cout << "time_bmax cost time = " << time_bmax / 1000.0 << " ms" << endl; 
	cout << "q_sum = " << q_sum << endl;
	fout.close();



	//----------------------------TS---------------------------------
	//float delta = 300;
	//RoadGraph* road_graph = new RoadGraph(delta);
	//SocialGraph* social_graph = new SocialGraph();
	//BTree* b_tree = new BTree(road_graph , delta);
	//b_tree = new BTree(road_graph, delta);
	//b_tree->createTree();
	//fstream fin(social_query);
	//int query_num;
	//fin >> query_num;
	//ofstream fout(gen_qRegion);
	//ofstream fout1(countTopK_TS);
	//float total_query = 0;
	//float total_bmax = 0;
	//int nodeId, start_time;
	//float total_topk = 0;
	//int count = 0;
	//TestGrady* grady = new TestGrady(social_graph);
	//RoadConSocial* road_social = new  RoadConSocial();
	//int q_sum = 0;
	//while (fin >> nodeId >> start_time) {
	//	int topK = 20;
	//	unordered_set<int> result = b_tree->root->bidirectionSearch(nodeId, start_time, delta, 900, fout,total_bmax , total_query);
	//	clock_t tbegin1 = clock(), tend1;
	//	unordered_set<int> target;
	//	for (int i = 0; i < social_graph->node_num; i++) {
	//		int ro = road_social->s_c_r[i];
	//		if (result.find(ro) != result.end()) {
	//			target.emplace(i);
	//		}
	//	}
	//	int size = target.size();
	//	int sum = 0;
	//	if (size == 0) {
	//		fout1 << 0 << endl;
	//		continue;
	//	} 
	//	grady->findTopK(topK, target);
	//	for(auto num : grady->com_topk){
	//		sum += num.second;
	//	}
	//	fout1 << sum << endl;
	//	grady->com_topk.clear();
	//	for (int id = 0; id < social_graph->node_num; id++) {
	//		social_graph->nodes[id]->binf = 0;
	//		social_graph->nodes[id]->canInfulence.clear();
	//		social_graph->nodes[id]->beInfluence.clear();
	//	}
	//	tend1 = clock();
	//	total_topk += (tend1 - tbegin1);
	//}
	//cout << "bidRearch cost time = " << total_query / 1000.0 << " ms   " << "topk cost = " << total_topk / 1000.0 << " ms   total = " << (total_query + total_topk) / 1000.0 << "ms" << endl;
	//cout << "time_bmax cost time = " << total_bmax / 1000.0 << " ms" << endl;
	//cout << "q_sum = " << q_sum << endl;
	return 0;
}
